<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Finance2 extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $status = input('param.status/d');
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            //type 1助力返积分 2订单使用 3提现 4订单退回
            $filter = input('filter');

            $where2[] = ['mf.type', 'in', [3]];
            if ($filter) {
                $filter = json_decode($filter, true);

                if (isset($filter['id'])) {
                    $where2[] = ['mf.id', '=', $filter['id']];
                }
                if (isset($filter['status'])) {
                    $where2[] = ['mf.status', '=', $filter['status']];
                }
                if (isset($filter['is_kuan'])) {
                    $where2[] = ['mf.is_kuan', '=', $filter['is_kuan']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where2[] = ['mf.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['status_time'])) {
                    $status_time = explode(' - ', $filter['status_time']);
                    foreach ($status_time as $k => $v) {
                        $status_time[$k] = strtotime($v);
                    }
                    $where2[] = ['mf.status_time', 'between', [$status_time[0], $status_time[1]]];
                }
                if (isset($filter['kuan_time'])) {
                    $status_time = explode(' - ', $filter['kuan_time']);
                    foreach ($status_time as $k => $v) {
                        $status_time[$k] = strtotime($v);
                    }
                    $where2[] = ['mf.kuan_time', 'between', [$status_time[0], $status_time[1]]];
                }
            }
            $_list = Db::name('member_coupon1')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->field('mf.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where2)
                ->order('create_time desc')
                ->page($page, $limit)
                ->select();
            $total = Db::name('member_coupon1')
                ->alias('mf')
                ->join('member m', 'mf.member_id=m.id')
                ->where($where2)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['status_time']) {
                    $_list[$k]['status_time'] = date('Y-m-d H:i:s', $v['status_time']);
                } else {
                    $_list[$k]['status_time'] = '待审核';
                }

                if ($_list[$k]['kuan_time']) {
                    $_list[$k]['kuan_time'] = date('Y-m-d H:i:s', $v['kuan_time']);
                } else {
                    $_list[$k]['kuan_time'] = '待打款';
                }
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        if (isset($status)) {
            $this->assign('status', $status);
        }
        return $this->fetch('');
    }

    public function edit()
    {

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update = array(
                'id' => $data['id'],
                'status' => $data['status'],
            );
            if ($data['status']==1){
                $update['status_time']=time();
            }
            $num = Db::name('member_coupon1')->update($update);
            $this->success("提交成功", url("index"));
        }
        $finance_id = $this->request->param('id/d', 0);
        $data=Db::name('member_coupon1')->find($finance_id);
        switch ($data['tixian_type']) {
            case 1:
                $data['tixian_type_name'] = '银行提现';
                break;
            case 2:
                $data['tixian_type_name'] = '支付宝';
                break;
            case 3:
                $data['tixian_type_name'] = '微信提现';
                break;
            default:
                $data['tixian_type_name'] = '其他';
        }
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $this->assign("data", $data);
        $this->assign("is_detail", 0);
        return $this->fetch('');
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update = array(
                'id' => $data['id'],
                'status' => $data['status'],
            );
            if ($data['status']==1){
                $update['status_time']=time();
            }
            $num = Db::name('member_coupon1')->update($update);

            $this->success("提交成功", url("index"));
        }
        $finance_id = $this->request->param('id/d', 0);
        $data=Db::name('member_coupon1')->find($finance_id);
        switch ($data['tixian_type']) {
            case 1:
                $data['tixian_type_name'] = '银行提现';
                break;
            case 2:
                $data['tixian_type_name'] = '支付宝';
                break;
            case 3:
                $data['tixian_type_name'] = '微信提现';
                break;
            default:
                $data['tixian_type_name'] = '其他';
        }
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);

        if ($data['status_time']) {
            $data['status_time'] = date('Y-m-d H:i:s', $data['status_time']);
        } else {
            $data['status_time'] = '待审核';
        }

        if ($data['kuan_time']) {
            $data['kuan_time'] = date('Y-m-d H:i:s', $data['kuan_time']);
        } else {
            $data['kuan_time'] = '待打款';
        }

        $this->assign("data", $data);
        $this->assign("is_detail", 1);
        return $this->fetch('');
    }
    public function ckuan(){
        $id = $this->request->param('id/d', null);
        $finance_info=Db::name('member_coupon1')->find($id);
        if ($finance_info['status']==0){
            $this->error('未审核，不可转账！');
        }
        if ($finance_info['is_kuan']==1){
            $this->error('当前已转账！');
        }
        $update=array(
            'id'=>$id,
            'kuan_time'=>time(),
            'is_kuan'=>1
        );
        $result=Db::name('member_coupon1')->update($update);
        if ($result){
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
}